package cn.rui.service.impl;

import cn.rui.mapper.OrderMapper;
import cn.rui.mapper.ReportMapper;
import cn.rui.service.ReportService;
import cn.rui.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 徽州大都督
 * @date 2020/8/30
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Integer> findByMonths(List<String> months) {

        List<Integer> memberCount = new ArrayList<> ();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = reportMapper.findMemberCountBeforeDate (date);
            memberCount.add (count);
        }
        return memberCount;
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {

        Map<String, Object> map = new HashMap<> ();
        //当前日期
        String reportDate = DateUtils.parseDate2String (new Date ());

        //本日新增会员数
        Integer todayNewMember = reportMapper.findMemberCountByDate (reportDate);

        //总会员数
        Integer totalMember = reportMapper.findMemberTotalCount ();

        //本周新增会员数
        //获取当前周的周一日期
        String mondayStr = DateUtils.parseDate2String (DateUtils.getThisWeekMonday (DateUtils.getToday ()));
        Integer thisWeekNewMember = reportMapper.findMemberCountAfterDate (mondayStr);

        //获取当月新增会员数
        //获取当月1号的日期
        String monthStr = DateUtils.parseDate2String (DateUtils.getFirstDay4ThisMonth ());
        Integer thisMonthNewMember = reportMapper.findMemberCountAfterDate (monthStr);

        //获取当天预约的会员数
        Integer todayOrderNumber = orderMapper.findOrderCountByDate (reportDate);

        //今日到诊数量
        Integer todayVisitsNumber = orderMapper.findVisitsCountByDate (reportDate);

        //本周预约会员数
        Integer thisWeekOrderNumber = orderMapper.findOrderCountAfterDate (mondayStr);

        //本周预约到诊数
        Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate (mondayStr);

        //本月预约会员数
        Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate (monthStr);

        //本月预约到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate (monthStr);

        //热门套餐及预约数量比例
        List<Map<String, Object>> hotSetmeal = orderMapper.findHotSetmeal ();
        map.put ("reportDate", reportDate);
        map.put ("todayNewMember", todayNewMember);
        map.put ("totalMember", totalMember);
        map.put ("thisWeekNewMember", thisWeekNewMember);
        map.put ("thisMonthNewMember", thisMonthNewMember);
        map.put ("todayOrderNumber", todayOrderNumber);
        map.put ("todayVisitsNumber", todayVisitsNumber);
        map.put ("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put ("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put ("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put ("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put ("hotSetmeal", hotSetmeal);
        return map;
    }

}
